package com.co.notaweb.beans.system;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.event.PhaseId;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import com.co.notaweb.constantes.ConstantesMsg;
import com.co.notaweb.entities.Docente;
import com.co.notaweb.entities.Estudiante;
import com.co.notaweb.entities.Persona;
import com.co.notaweb.entities.PersonaGenerica;
import com.co.notaweb.entities.system.Rol;
import com.co.notaweb.facade.IColegioFacadeLocal;
import com.co.notaweb.facade.system.ISistemaFacadeLocal;
import com.co.notaweb.util.Constants;
import com.co.notaweb.util.FacesUtils;
import com.co.notaweb.util.SelectUtil;
import com.co.notaweb.util.ServiceLocator;
import com.icesoft.faces.component.ext.RowSelectorEvent;

public class PersonaBean {

	private ISistemaFacadeLocal sistemaFacade;
	private IColegioFacadeLocal colegioFacade;
	private Persona persona;
	private Estudiante estudiante;
	private Docente docente;
	private long idRol;
	private List<Rol> roles;
	private PersonaGenerica personaGenerica;
	private List<SelectItem> listaTiposPersona;
	private List<Persona> listaPersonas;
	private List<SelectItem> listaRoles;
	private String tipoPersona = "";
	private int personaSelected = 0;

	/**
	 * personaBean
	 * 
	 * @return
	 */

	@PostConstruct
	private void init() {
		try {
			sistemaFacade = (ISistemaFacadeLocal) new ServiceLocator()
					.getServiceLocal(Constants.FACADESISTEMA);

			colegioFacade = (IColegioFacadeLocal) new ServiceLocator()
					.getServiceLocal(Constants.FACADECOLEGIO);

			persona = new Persona();
			estudiante = new Estudiante();
			docente = new Docente();
			personaGenerica = new PersonaGenerica();
			roles = new ArrayList<Rol>();
			listaTiposPersona = SelectUtil.buildTiposPersonaItems();
			listaPersonas = colegioFacade.consultarAllPersonas();
			listaRoles = SelectUtil.buildRolesItems(sistemaFacade
					.consultarAllRoles());

		} catch (Exception e) {
			FacesUtils.addErrorMessageBundle(ConstantesMsg.COMMON_ERROR);
			e.printStackTrace();
		}
	}

	private void limpiarForm() {
		persona = new Persona();
		tipoPersona = "0";
		personaSelected = 0;
		idRol = 0;
	}

	private void refreshList() {
		try {
			listaPersonas = colegioFacade.consultarAllPersonas();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void seleccionarTipoPersona(Persona persona) {
		if (persona instanceof Estudiante) {
			tipoPersona = Constants.PERSONA_TIPO_ESTUDIANTE;
		} else if (persona instanceof Docente) {
			tipoPersona = Constants.PERSONA_TIPO_DOCENTE;
		} else if (persona instanceof PersonaGenerica) {
			tipoPersona = Constants.PERSONA_TIPO_GENERICA;
		}
	}

	/**
	 ******************************************************************************************************************** 
	 * Actions
	 ******************************************************************************************************************** 
	 */

	public String nuevo() {
		limpiarForm();
		return "";
	}

	public String crear() {
		try {
			if (!validarForm() || persona == null) {
				FacesUtils
						.addErrorMessageBundle(ConstantesMsg.COMMON_REQUIRED_VALUE);
				return "";
			}
			colegioFacade.createPersona(persona, idRol, tipoPersona);
			refreshList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public String guardar() {
		try {
			if (!validarForm()) {
				FacesUtils
						.addErrorMessageBundle(ConstantesMsg.COMMON_REQUIRED_VALUE);
				return "";
			}
			refreshList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public String eliminar() {
		try {
			if (persona.getId() > 0) {
				refreshList();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 ******************************************************************************************************************** 
	 * Event Listners
	 ******************************************************************************************************************** 
	 */

	public void selectRolListener(ValueChangeEvent event) {
		if (event.getPhaseId() != PhaseId.INVOKE_APPLICATION) {
			event.setPhaseId(PhaseId.INVOKE_APPLICATION);
			event.queue();
			return;
		}
	}

	public void rowPersonaSelectionListener(RowSelectorEvent event) {
		if (event.getPhaseId() != PhaseId.INVOKE_APPLICATION) {
			event.setPhaseId(PhaseId.INVOKE_APPLICATION);
			event.queue();
			return;
		}
		try {
			persona = listaPersonas.get(personaSelected);
			seleccionarTipoPersona(persona);
			idRol = persona.getUsuario().getRoles().get(0).getId();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 ******************************************************************************************************************** 
	 * Private Methods
	 ******************************************************************************************************************** 
	 */

	private boolean validarForm() {
		if (tipoPersona.isEmpty() || tipoPersona.equalsIgnoreCase("0")) {
			return false;
		}
		if (idRol <= 0) {
			return false;
		}

		return true;
	}

	/**
	 ******************************************************************************************************************** 
	 * Getters And Setters
	 ******************************************************************************************************************** 
	 */

	public Persona getPersona() {
		return persona;
	}

	public void setPersona(Persona persona) {
		this.persona = persona;
	}

	public List<SelectItem> getListaTiposPersona() {
		return listaTiposPersona;
	}

	public void setListaTiposPersona(List<SelectItem> listaTiposPersona) {
		this.listaTiposPersona = listaTiposPersona;
	}

	public List<Persona> getListaPersonas() {
		return listaPersonas;
	}

	public void setListaPersonas(List<Persona> listaPersonas) {
		this.listaPersonas = listaPersonas;
	}

	public List<SelectItem> getListaRoles() {
		return listaRoles;
	}

	public void setListaRoles(List<SelectItem> listaRoles) {
		this.listaRoles = listaRoles;
	}

	public String getTipoPersona() {
		return tipoPersona;
	}

	public void setTipoPersona(String tipoPersona) {
		this.tipoPersona = tipoPersona;
	}

	public int getPersonaSelected() {
		return personaSelected;
	}

	public void setPersonaSelected(int personaSelected) {
		this.personaSelected = personaSelected;
	}

	public long getIdRol() {
		return idRol;
	}

	public void setIdRol(long idRol) {
		this.idRol = idRol;
	}

}
